/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.jiujiudevice.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.jiujiudevice.entity.JiuFreeTime;
import com.boxin.ims.modules.jiujiudevice.service.JiuFreeTimeService;

/**
 * 设备免费配置Controller
 * @author sxh
 * @version 2018-11-24
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/jiujiudevice/jiuFreeTime")
public class JiuFreeTimeController extends BaseController {

	private static final Long FREE_ID = 1L;//配置唯一
	@Autowired
	private JiuFreeTimeService jiuFreeTimeService;
	
	@ModelAttribute
	public JiuFreeTime get() {
			return jiuFreeTimeService.get(1L);
	}
	
	@RequiresPermissions("jiujiudevice:jiuFreeTime:view")
	@RequestMapping(value = {"list", ""})
	public String list(JiuFreeTime jiuFreeTime, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
        Page<JiuFreeTime> page = jiuFreeTimeService.find(new Page<JiuFreeTime>(request, response), jiuFreeTime); 
        model.addAttribute("page", page);
		return "modules/jiujiudevice/jiuFreeTimeList";
	}

	@RequiresPermissions("jiujiudevice:jiuFreeTime:view")
	@RequestMapping(value = "form")
	public String form(JiuFreeTime jiuFreeTime, Model model) {
	
		User user = UserUtils.getUser();
		model.addAttribute("jiuFreeTime", jiuFreeTime);
		return "modules/jiujiudevice/jiuFreeTimeForm";
	}

	@RequiresPermissions("jiujiudevice:jiuFreeTime:edit")
	@RequestMapping(value = "save")
	public String save(JiuFreeTime jiuFreeTime, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, jiuFreeTime)){
			return form(jiuFreeTime, model);
		}
		JiuFreeTime free = jiuFreeTimeService.get(FREE_ID);
		if(free != null){
			free.setCreateTime(new Date());
			free.setMin(jiuFreeTime.getMin());
			jiuFreeTimeService.save(free);
		}else{
			jiuFreeTimeService.save(jiuFreeTime);
		}
		addMessage(redirectAttributes, "保存设备免费配置成功");
		return "redirect:"+Global.ADMIN_PATH+"/jiujiudevice/jiuFreeTime/form?repage";
	}
	
	@RequiresPermissions("jiujiudevice:jiuFreeTime:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		jiuFreeTimeService.delete(id);
		addMessage(redirectAttributes, "删除设备免费配置成功");
		return "redirect:"+Global.ADMIN_PATH+"/jiujiudevice/jiuFreeTime/?repage";
	}

}
